<template>
  <div>
    <!-- 按钮开始 -->
    <el-button @click="addHandler" type="primary" size="mini">新增</el-button>
    <!-- 按钮结束 -->
    <!-- 表格开始 -->
    <el-table :data="syatemData" style="width: 100%">
      <el-table-column prop="id" label="序号" width="50px" align="center">
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center">
      </el-table-column>
      <el-table-column prop="val" label="值" align="center"> </el-table-column>
      <el-table-column prop="introduce" label="介绍" align="center">
      </el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            @click="editHandler(scope.row)"
            type="primary"
            align="center"
            size="mini"
            >编辑</el-button
          >
          <el-button
            @click="deleteHandler(scope.row.id)"
            type="danger"
            align="center"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <!-- 表单开始 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="introduce">
          <el-input v-model="ruleForm.introduce"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="region">
          <el-select
            @change="changeHandler"
            v-model="ruleForm.region"
            placeholder="请选择活动区域"
          >
            <el-option label="文字" value="shanghai"></el-option>
            <el-option label="图片" value="beijing"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-show="show" label="值" prop="val">
          <el-input v-model="ruleForm.val"></el-input>
        </el-form-item>
        <el-form-item v-show="!show" label="图片上传" prop="val">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :show-file-list="false"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <!-- 表单结束 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { get, post } from "../../utils/request.js";
export default {
  data() {
    return {
      syatemData: [],
      dialogFormVisible: false,
      ruleForm: {
        name: "",
        introduce: "",
        region: "",
      },
      title: "新增基础配置",
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "请输入介绍内容", trigger: "blur" },
        ],
        region: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
      },
      show: false,
      imageUrl:''
    };
  },
  computed: {},
  methods: {
    
    // 查询所有配置信息
    findAllSystem() {
      get("/baseConfig/findAll").then((res) => {
        // console.log(res.data);
        if ((res.status = 200)) {
          this.syatemData = res.data;
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    // 点击新增按钮
    addHandler() {
      this.dialogFormVisible = true;
      this.ruleForm = {};
    },
    // 提交保存
    submitHandler() {
      // console.log(this.ruleForm);
      post("/baseConfig/saveOrUpdate", this.ruleForm).then((res) => {
        if (res.status == 200) {
          this.dialogFormVisible = false;
          this.findAllSystem(this.ruleForm);
          this.$message({
            type: "success",
            message: res.message,
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
      });
    },
    // 编辑栏目信息
    editHandler(data) {
      // 将当前行的内容赋值给ruleForm
      this.ruleForm = data;
      this.title = "编辑栏目信息";
      // 打开模态框
      this.dialogFormVisible = true;
    },
    // 删除栏目信息
    deleteHandler(id) {
      this.$confirm("此操作将永久删除该用户信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        // await等待后面异步函数运行完毕，拿到响应的数据，然后返回
        let res = await get("/baseConfig/deleteById", { id });
        this.$notify({
          title: res.status == 200 ? "删除成功" : "删除失败",
          message: res.statusText,
          type: res.status == 200 ? "success" : "error",
        });
        this.findAllSystem();
      });
    },
    // 判断
    changeHandler() {
      this.show = !this.show;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.findAllSystem();
  },
  mounted() {},
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>